﻿using cal24.ViewModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace cal24
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainViewModel mainViewModel { get; set; }
        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            mainViewModel = new MainViewModel();
            DataContext = mainViewModel;
        }

        private void reCal()
        {
            btn_num1.IsEnabled = true;
            btn_num2.IsEnabled = true;
            btn_num3.IsEnabled = true;
            btn_num4.IsEnabled = true;
            mainViewModel.prepareLeftNumber = false;
            mainViewModel.prepareCal = false;
            mainViewModel.displayOut = "";
            btn_M.Content = "M";
            btn_help.Content = "?";
            btn_help.FontSize = 50;
            mainViewModel.answerIndex = 0;
        }

        private void btn_click(object sender, RoutedEventArgs e)
        {
            btn_help.Content = "?";
            btn_help.FontSize = 50;
            mainViewModel.answerIndex = 0;

            Button btn = sender as Button;
            string s = btn.Content.ToString();
            if(mainViewModel.calbtn != null)
            {
                mainViewModel.calbtn.Background = btn_M.Background;
            }
            switch (s)
            {
                case "+":
                case "-":
                case "×":
                case "÷":
                    mainViewModel.prepareCal = true;
                    mainViewModel.calString = s;
                    mainViewModel.calbtn = btn;
                    btn.Background = new SolidColorBrush(Colors.DarkOliveGreen);
                    break;
                case "M":
                    if (mainViewModel.prepareLeftNumber)
                    {
                        mainViewModel.prepareLeftNumber = false;
                        mainViewModel.prepareCal = false;
                        btn.Content = mainViewModel.leftNumber;
                        mainViewModel.displayOut = "";
                    }
                    break;
                case "C":
                    reCal();
                    break;
                default:
                    if(btn == btn_M)
                    {
                        if((mainViewModel.prepareCal == true && mainViewModel.prepareLeftNumber==false)
                            || (mainViewModel.prepareCal == false && mainViewModel.prepareLeftNumber == true))
                        {
                            break;
                        }
                    }

                    if (mainViewModel.prepareLeftNumber == false)
                    {
                        mainViewModel.prepareLeftNumber = true;
                        btn.IsEnabled = false;
                        mainViewModel.leftNumber = int.Parse(s);
                    }
                    else if (mainViewModel.prepareCal)
                    {
                        mainViewModel.prepareCal = false;
                        btn.IsEnabled = false;
                        mainViewModel.rightNumber = int.Parse(s);
                        switch (mainViewModel.calString)
                        {
                            case "+":
                                mainViewModel.leftNumber = mainViewModel.leftNumber + mainViewModel.rightNumber;
                                break;

                            case "-":
                                if (mainViewModel.leftNumber - mainViewModel.rightNumber < 0)
                                {
                                    reCal();
                                    break;
                                }
                                mainViewModel.leftNumber = mainViewModel.leftNumber - mainViewModel.rightNumber;
                                break;

                            case "×":
                                mainViewModel.leftNumber = mainViewModel.leftNumber * mainViewModel.rightNumber;
                                break;
                            case "÷":
                                if (mainViewModel.leftNumber % mainViewModel.rightNumber != 0)
                                {
                                    reCal();
                                    break;
                                }
                                mainViewModel.leftNumber = mainViewModel.leftNumber / mainViewModel.rightNumber;
                                break;
                        }
                    }
                    if (btn == btn_M)
                    {
                        btn.Content = "M";
                        btn.IsEnabled = true;
                    }
                    mainViewModel.displayOut = mainViewModel.leftNumber.ToString();
                    break;
            }

            if (mainViewModel.leftNumber == 24 && btn_num1.IsEnabled == false && btn_num2.IsEnabled == false && btn_num3.IsEnabled == false && btn_num4.IsEnabled == false && btn_M.Content.ToString() == "M")
            {
                reCal();
                mainViewModel.displayOut = "正确";
                mainViewModel.createGame();
            }
        }

        private void showAnswer(object sender, RoutedEventArgs e)
        {
            int index = mainViewModel.answerIndex % mainViewModel.answer.Count;
            btn_help.Content = string.Format("{0}/{1}", index + 1, mainViewModel.answer.Count);
            btn_help.FontSize = 24;
            mainViewModel.displayOut = mainViewModel.answer[index];
            mainViewModel.answerIndex++;

        }

        private void btn_next_click(object sender, RoutedEventArgs e)
        {
            reCal();
            mainViewModel.createGame();
        }
    }
}